#!/usr/bin/env sh

# Copyright (c) 2018 noamcore. All rights reserved.

function conf
{
  font() {
    NAMES=("TopazPlus" "MicroKnightPlus" "mOsOul" "Go Mono" "ypn envypn" "Lucy Tewi2a" "Iosevka" "Iosevka Term" "M+ 1mn" "scientifica" "curie" "t cherry" "artwiz lemon" "kakwafont" "Fantasque Sans Mono" "RobotoMono Nerd Font" "xos4 Terminus" "FuraCode Nerd Font" "Dina" "TamzenForPowerline")

    change_font() {
      size=${2:-12}
      sed -i "s/^st.font:.*/st.font: ${1}:pixelsize=${size}:antialias=true:autohint=true/" ~/.xrdb/st
    }

    PS3='Selecione a fonte: '
    select font_name in "${NAMES[@]}"; do
      case "$font_name" in
        TopazPlus) change_font "TopazPlus a600a1200a4000"; break ;;
        MicroKnightPlus) change_font "MicroKnightPlus"; break ;;
        mOsOul) change_font "mOsOul"; break ;;
        "Lucy Tewi2a") change_font "lucy tewi2a" 11; break ;;
        curie) change_font "$font_name" 12; break;;
        scientifica) change_font "$font_name" 11; break;;
        *cherry) change_font "$font_name" 13; break ;;
        *lemon) change_font "$font_name" 10; break ;;
        Iosevka*) change_font "$font_name" 14; break ;;
        kakwafont) change_font "$font_name" 12; break ;;
        Fantasque*) change_font "$font_name" 18; break ;;
        *Fura*) change_font "$font_name" 14; break ;;
        Dina) change_font "$font_name" 10; break ;;
        *envypn) change_font "$font_name" 15; break ;;
        *Terminus) change_font "$font_name" 16; break ;;
        TamzenForPowerline) change_font "$font_name" 14; break;;
        *) change_font "$font_name"; break ;;
      esac
    done

    cat ~/.xrdb/st | awk '{ $2 = "\t" $2; print }' | column -t -s $'\t' | tee ~/.xrdb/st &>/dev/null

    xrdb ~/.Xresources

    return 0
  }

  cursor() {
    PS3='Selecione o cursor: '
    select cursor_name in $(ls /usr/share/icons); do
      case "$cursor_name" in
        *)
          sed -i "s/\(Inherits=\).*/\1$cursor_name/" ~/.icons/default/index.theme
          sed -i "s/\(gtk-cursor-theme-name=\).*/\1$cursor_name/" ~/.config/gtk-3.0/settings.ini
          xsetroot -cursor_name left_ptr
          break
          ;;
      esac
    done
  }

  if [ $# -eq 0 ]; then
    show_help
  else
    case $1 in
      tmux)    nvim ~/.tmux.conf ;;
      nvim)    nvim ~/.config/nvim/init.vim ;;
      xi)      nvim ~/.xinitrc ;;
      xr)      nvim ~/.Xresources && xrdb ~/.Xresources ;;
      mksh)    nvim ~/.mkshrc && source ~/.mkshrc ;;
      func)    nvim ~/.functions && source ~/.mkshrc ;;
      bspwm)   nvim ~/.config/bspwm/bspwmrc ;;
      herbs)   nvim ~/.config/herbstluftwm/autostart ;;
      sxhkd)   nvim ~/.config/sxhkd/sxhkdrc ;;
      poly)    nvim ~/.config/polybar/config && killall -USR1 polybar ;;
      font)    font && xrdb ~/.Xresources && killall -USR1 xst ;;
      cursor)  cursor ;;
      func)    nvim "$0" && source "$0" ;;
      *)       echo "unknown conf: $1" ;;
    esac
  fi
}

mem() {
  total=$(free -m | awk '{print $2}' | sed -n '2p')
  used=$(free -m | awk '{print $3}' | sed -n '2p')

  printf "Memory Usage\n\nTotal: $total\nUsed: $used\n\n"
}

clip_to_img() {
  xclip -selection clipboard -t image/png -o > clipboard.png
}

noam() {
  case "$1" in
    clear)
      doas pacman -Rscn $(pacman -Qtdq)
      ;;
    recs)
      modprobe snd-aloop pcm_substreams=1
      ffmpeg -y -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 -f alsa -ac 2 -ar 44100 -i loopout output.mkv
      #screenkey | ffmpeg -y -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 -f alsa -ac 2 -ar 44100 -i loopout -f alsa -ac 2 -i hw:1,0 output.mkv
      #screenkey --no-systray -s small | ffmpeg -y -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 -f alsa -ac 2 -i hw:1 output.mkv
      ;;
    prev)
      ffmpeg -f v4l2 -video_size 640x480 -framerate 30 -i /dev/video0 -c:v libx264 -preset ultrafast -qp 0 -f avi 2>/dev/null - | ffplay -loglevel 0 -i pipe:0
  esac
}

function upload
{
  if [ "$#" -le 1 ]; then
      echo "Illegal number of parameters"
      exit
  fi

  rclone copy "$1" d:/"$2" -P
}

# Ref: https://github.com/paulmillr/dotfiles/blob/master/home/.zshrc.sh#L282
# Show how much RAM application uses.
# $ ram safari
# # => safari uses 154.69 MBs of RAM.
ram() {
  local sum
  local items
  local app="$1"
  if [ -z "$app" ]; then
    echo "First argument - pattern to grep from processes"
  else
    sum=0
    for i in `ps aux | grep -i "$app" | grep -v "grep" | awk '{print $6}'`; do
      sum=$(($i + $sum))
    done
    sum=$(echo "scale=2; $sum / 1024.0" | bc)
    if [[ $sum != "0" ]]; then
      echo "${fg[blue]}${app}${reset_color} uses ${fg[green]}${sum}${reset_color} MBs of RAM."
    else
      echo "There are no processes with pattern '${fg[blue]}${app}${reset_color}' are running."
    fi
  fi
}

pwgen() {
  head /dev/urandom | tr -dc 'a-zA-Z0-9.,[$:-?~!^_\\\[\]]' | head -c${1:-22}; echo
}

function pacman_unique()
{
  pactree -u "$1" | wc -l
}

# vim: ft=sh
